CSharpTest.Net
TryAdd(TKey,Converter<TKey,TValue>) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.BPlusTree Assembly > CSharpTest.Net.Collections Namespace > BPlusTree<TKey,TValue> Class > TryAdd Method : TryAdd(TKey,Converter<TKey,TValue>) Method

key
fnCreate

Glossary Item Box

Adds an element with the provided key and value to the IDictionary by calling the provided factory method to construct the value if the key is not already present in the collection.

Syntax

Visual Basic (Declaration) 
Public Overloads Function TryAdd( _
   ByVal key As TKey, _
   ByVal fnCreate As Converter(Of TKey,TValue) _
) As Boolean
C# 
public bool TryAdd( 
   TKey key,
   Converter<TKey,TValue> fnCreate
)

Parameters

key
fnCreate

Example

BPlusTree/BPlusTree.Test/ThreadedBTreeTest.cs

C#Copy Code
const int CountPerThread = 100;

BPlusTree<KeyInfo, DataValue>.OptionsV2 options = new BPlusTree<KeyInfo, DataValue>.OptionsV2(
    new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());
options.CalcBTreeOrder(32, 300);
options.FileName = TempFile.TempPath;
options.CreateFile = CreatePolicy.Always;

using (BPlusTree<KeyInfo, DataValue> dictionary = new BPlusTree<KeyInfo, DataValue>(options))
using (WorkQueue work = new WorkQueue(Environment.ProcessorCount))
{
    Exception lastError = null;
    work.OnError += delegate(object o, ErrorEventArgs e) { lastError = e.GetException(); };

    for (int i = 0; i < Environment.ProcessorCount; i++)
        work.Enqueue(new ThreadedTest(dictionary, CountPerThread).Run);

    for (int i = 0; i < CountPerThread; i++)
    {
        if(i%2 == 0)
        {
            try {
                dictionary.TryAdd(new KeyInfo(Guid.NewGuid(), i), k => { throw new ExpectedException(); });
            } catch { }
        }
        else
        {
            try
            {
                dictionary.TryRemove(dictionary.First().Key, (k, v) => { throw new ExpectedException(); });
            }
            catch { }
        }
    }

    Assert.IsTrue(work.Complete(true, 60000));
    Assert.IsNull(lastError, "Exception raised in worker: {0}", lastError);
}
VB.NETCopy Code
Const  CountPerThread As Integer = 100

        Dim options As New BPlusTree(Of KeyInfo, DataValue).OptionsV2(New KeyInfoSerializer(), New DataValueSerializer(), New KeyInfoComparer())
        options.CalcBTreeOrder(32, 300)
        options.FileName = TempFile.TempPath
        options.CreateFile = CreatePolicy.Always

        Using dictionary As New BPlusTree(Of KeyInfo, DataValue)(options)
            Using work As New WorkQueue(Environment.ProcessorCount)
                Dim lastError As Exception = Nothing
                work.OnError += Function(o As Object, e As ErrorEventArgs) Do
                    lastError = e.GetException()
                End Function

                Dim i As Integer = 0
                While i < Environment.ProcessorCount
                    work.Enqueue(New ThreadedTest(dictionary, CountPerThread).Run)
                    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
                End While

                Dim i As Integer = 0
                While i < CountPerThread
                    If i Mod 2 = 0 Then
                        Try
                            dictionary.TryAdd(New KeyInfo(Guid.NewGuid(), i), Function(k) 
                            Throw New ExpectedException()

End Function)
                        Catch
                        End Try
                    Else
                        Try
                            dictionary.TryRemove(dictionary.First().Key, Function(k, v) 
                            Throw New ExpectedException()

End Function)
                        Catch
                        End Try
                    End If
                    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
                End While

                Assert.IsTrue(work.Complete(True, 60000))
                Assert.IsNull(lastError, "Exception raised in worker: {0}", lastError)
            End Using
        End Using

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys